package com.zegome.game.oanquan.scene.play;

import android.view.KeyEvent;

import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.zegome.game.entity.sprite.Sprite;
import com.zegome.game.main.AndroidGameController;
import com.zegome.game.oanquan.GameCenter;
import com.zegome.game.oanquan.ImageAssets;
import com.zegome.game.oanquan.OAnQuanController;
import com.zegome.game.oanquan.SoundAsset;
import com.zegome.game.oanquan.scene.OAnQuanScreen;
import com.zegome.game.screen.ITranScreenAction;

/**
 * (C) 2013 ZeroTeam
 * 
 * @author QuanLT
 */

public class PlayingScreen extends OAnQuanScreen{

	// ===========================================================
	// Constants
	// ===========================================================
	public static final int NONE = -1;
	public static final int PLAYING = 0;
	public static final int RESUME = 1;
	public static final int PAUSE = 2;
	public static final int RESULT = 3;
	public static final int SAVE_SCORE = 4;
	public static final int SHOW_HIGH_SCORE = 5;
	
	public static final byte DRAW = -1;
	public static final byte FIRST_PLAYER = 0;
	public static final byte SECOND_PLAYER = 1;
	
	// ===========================================================
	// Fields
	// ===========================================================
	
	private BoardManager mBoardManager;
	
	private Music mBgMusic;
	
	public int mState = NONE;
	
	// ===========================================================
	// Constructor
	// ===========================================================
	public PlayingScreen(AndroidGameController controller, String name) {
		super(controller, name);
		mController = (OAnQuanController)mController;
	}

	// ===========================================================
	// Getter & Setter
	// ===========================================================
	public int getScore() {
		return mBoardManager.getScore();
	}
	
	public int getHighScoreIndex() {
		return mBoardManager.getHighScoreIndex();
	}
	
	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();

		final TextureRegion bgRegion = mController.get(ImageAssets.ASSET_FOLDER_ATLAS + ImageAssets.ATLAS_BG_GAME, TextureAtlas.class).findRegion(ImageAssets.REGION_BG_GAME);
		
		final Sprite bg = new Sprite(0, 0, bgRegion);
		bg.setZIndex(-1);
		attachChild(bg);
		
//		mPlayingUI = new PlayingUI(this);
		mBoardManager = new BoardManager(this, GameCenter.BOARD_X, GameCenter.BOARD_Y);
		mBgMusic = mController.get(SoundAsset.ASSET_MUSIC_FOLDER + SoundAsset.ASSET_MUSIC_BOARD, Music.class);
		mBgMusic.setLooping(true);
	}
		
	@Override
	public void nextScreen() {
		
	}

	@Override
	public void backScreen() {
		// TODO Auto-generated method stub
			transitionScreen(new ITranScreenAction() {
			
			@Override
			public void onLoadComplete() {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onLoad() {
				mBoardManager.clearAction();
				if (GameCenter.isMusic) mBgMusic.pause();
			}
			
			@Override
			public boolean isNeedUpdateAsset() {
				// TODO Auto-generated method stub
				return false;
			}
			
			@Override
			public void prepareNewScreen() {
				
			}
			
			@Override
			public String gotoScreen() {
//				mPlayingUI.clear();
				return GameCenter.SCREEN_MENU;
			}
		});
	}
	
	@Override
	public void changeMusicSetting(boolean isMusic) {
		if (isMusic) {
			if (mBgMusic.isPlaying()) return;
			mBgMusic.play();
		} else {
			if (mBgMusic.isPlaying()) mBgMusic.pause();
		}
	}
	
	@Override
	public void show() {
		// TODO Auto-generated method stub
		super.show();
		setup(true, true);
		mBoardManager.startGame();
		if (GameCenter.isMusic) mBgMusic.play();
	}
	
	@Override
	public boolean onKeyUp(int keyCode) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (!mUICenter.back()) {
				if (mState == PLAYING) {
					onGameScreenPause();
				}
			}
		}
		
		return false;
	}
	
	@Override
	public void pause() {
		mUICenter.pause();
		super.pause();
	}
	
	@Override
	public void resume() {
		mUICenter.resume();
		super.resume();
	}
	
	@Override
	public void onGameScreenPause() {
		if (GameCenter.isMusic) mBgMusic.play();
		if (mState == PLAYING) {
			mState = PAUSE;
			setup(false, true);
			mUICenter.showPause(true);
		}
	}
	
	@Override
	public void onGameScreenResume() {
		if (GameCenter.isMusic) mBgMusic.play();
	}
	
	// ===========================================================
	// Methods
	// ===========================================================
	public void continuePlaying() {
		if (GameCenter.isMusic) mBgMusic.play();
		setup(true, true);
		mState = PLAYING;
	}
	
	public void showResult(final byte winner, final int firstScore, final int secondScore) {
//		mPlayingUI.showResult(winner, firstScore, secondScore);
		mUICenter.showResult(winner, firstScore, secondScore);
	}

	@Override
	public void returnScreen() {
		super.returnScreen();
		if (GameCenter.isMusic) mBgMusic.play();
		mUICenter.setScreen(this);
		setup(true, true);
		mBoardManager.startGame();
	}

	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
}
